package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的图书的名字");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int index = -1;
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book temp = bookList.getBooks(i);
            if (temp.getName().equals(name)) {
                index = i;
                break;
            }
        }
            if(index < 0){
                System.out.println("你要删除的图书不存在！");
            }else {
            for (int j = index; j < currentSize-1; j++) {

                Book book = bookList.getBooks(j + 1);
                bookList.setBooks(book, j);
            }
            bookList.setBooks(null,currentSize-1);
            bookList.setUseSize(currentSize-1);
                System.out.println("删除成功！");
            }
        }
    }

